package com.xw.service.sysconfig;

import cn.hutool.crypto.SecureUtil;
import com.xw.dao.UserMapper;
import com.xw.model.User;
import com.xw.service.common.impl.BaseServiceImpl;
import com.xw.util.JsonData;
import com.xw.vo.UserVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author wangxiaoxiao
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public List<UserVo> findByWhere(UserVo user) {
        return userMapper.findByWhere(user);
    }

    @Override
    public Map add(User user) {
        //判断是否存在该用户
        User user1 = new User();
        user1.setUserLoginName(user.getUserLoginName());
        //系统用户
        user1.setUserType(2);
        List<User> users = userMapper.select(user1);
        if (!users.isEmpty()){
            return JsonData.fail("Account already exists");
//            return JsonData.fail("账号已存在");
        }
        //1.注册用户 2.系统管理员
        user.setUserType(2);
        user.setCreateTime(new Date());
        user.setUserPwd(SecureUtil.md5(user.getUserPwd()));
        user.setUserId(SecureUtil.simpleUUID());
        //1.正常 2.拉黑
        user.setUserStatus(1);
        Integer integer = userMapper.insertSelective(user);
        return JsonData.returnMap(integer);
    }

    @Override
    public Map dels(String... userIds) {
        Integer integer=userMapper.dels(userIds);
        return JsonData.returnMap(integer);
    }
}
